
import java.io.IOException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Javier
 */
public class MainMenu {

    Image imgButtons;
    Image imgTitle;
    Image imgInstructions;

    public final static int NUM_IMG_HEIGHT_BUTTONS= 9;

    public final static int MAIN_MENU= 0;
    public final static int INSTRUCTIONS_MENU= 1;
    public static int menu=0;


    private int NewGameButtonSprite=0;
    private int InstructionsButtonSprite=2;

    private int xNewGameButton;
    private int yNewGameButton;

    private int xInstructionsButton;
    private int yInstructionsButton;

    private int wButtons;
    private int hButtons;

    private int wTitle;
    private int hTitle;

    private int wInstructions;
    private int hInstructions;

    private int selectedButton=0;

    public final static int INTERVAL_CLICK= 75;
    public long timeLastClick;


    public MainMenu(){
        timeLastClick= MyCanvas.cycleStartTime;
        LoadImages();
    }
    
    public void LoadImages() {
        try {
            //load all the images in memory
            imgButtons = Image.createImage("/buttons.png");
            imgTitle = Image.createImage("/title.png");
            imgInstructions = Image.createImage("/instructions.png");

            //get the size of every image
            wButtons = imgButtons.getWidth();
            hButtons = imgButtons.getHeight()/NUM_IMG_HEIGHT_BUTTONS;

            wTitle = imgTitle.getWidth();
            hTitle = imgTitle.getHeight();

            wInstructions = imgInstructions.getWidth();
            hInstructions = imgInstructions.getHeight();

        } catch (IOException ex)
        { }
    }

    public void paint(Graphics g) {
        //draw the background
        g.drawImage(MyCanvas.imgBg, 0, 0, Graphics.LEFT|Graphics.TOP);

        //draw the actual menu
        if(menu == MAIN_MENU){
            paintMain(g);
        }
        else if(menu == INSTRUCTIONS_MENU){
            paintInstructions(g);
        }

    }

    public void paintMain(Graphics g){
        //draw the title of the game
        g.drawImage(imgTitle, 20, 40, Graphics.LEFT|Graphics.TOP);

        //draw the new game button
        g.setClip(26, 150, wButtons, hButtons);
        g.drawImage(imgButtons, 26, 150-hButtons*(NewGameButtonSprite), Graphics.LEFT|Graphics.TOP);

        //draw the instructions button
        g.setClip(26,  200, wButtons, hButtons);
        g.drawImage(imgButtons, 26, 200-hButtons*(InstructionsButtonSprite), Graphics.LEFT|Graphics.TOP);

        g.setClip(0, 0, MyCanvas.wCanvas, MyCanvas.hCanvas);
    }

    public void paintInstructions(Graphics g){
        //draw the game instructions
        g.drawImage(imgInstructions, 0, 0, Graphics.LEFT|Graphics.TOP);

        //draw the button for returning to the main menu
        g.setClip(26,  hInstructions+10, wButtons, hButtons);
        g.drawImage(imgButtons, 26, hInstructions+10-(hButtons<<2),Graphics.LEFT|Graphics.TOP);

        g.setClip(0, 0, MyCanvas.wCanvas, MyCanvas.hCanvas);
    }

    public void processTick() {
        //if enough time has passed since the last mouse click
        if(MyCanvas.cycleStartTime - timeLastClick > INTERVAL_CLICK){
            timeLastClick = MyCanvas.cycleStartTime;

            //update the selected button
            if(MyCanvas.moveUp==true){
                if(selectedButton>0){
                    selectedButton--;
                }
            }
            if(MyCanvas.moveDown==true){
                if(selectedButton<1){
                    selectedButton++;
                }
            }
            //if the player has pressed the fire button
            if(MyCanvas.fire==true){
                if(menu==MAIN_MENU){
                    //if the selected option was to start a new game it's loaded
                    if(selectedButton==0){
                        MyCanvas.classOption=MyCanvas.GAME_OPTION;
                    }
                    //if the selected option was the instructions menu it's drawn
                    else if(selectedButton==1){
                        menu=INSTRUCTIONS_MENU;
                    }
                }
                //change to the main menu
                else if(menu==INSTRUCTIONS_MENU){
                    menu=MAIN_MENU;
                }
            }

             //change the sprites if the selected button has changed
            if(selectedButton==0){
                NewGameButtonSprite=1;
                InstructionsButtonSprite=2;
            }
            else{
                NewGameButtonSprite=0;
                InstructionsButtonSprite=3;
            }
        }
    }

}
